//
//  OtherSettingView.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/7/15.
//

import SwiftUI
import UTEBluetoothRYApi
import ToastUI

struct OtherSettingView: View {
    @EnvironmentObject var appState: AppState
    @StateObject var viewModel = DeviceDetailVM()
    @State var showToast : Bool = false
    @State private var selectedTimeType: String = {
           let is24Hour = Locale.current.hourCycle == .zeroToTwentyThree
           return is24Hour ? "24小时制" : "12小时制"
       }()
    let times = ["12小时制", "24小时制"]
    @State private var iskzyy = false
    @State private var issdms = false
    @State private var istqxx = false
    @State private var issos = false
    @State private var isxlyj = false
    @State private var isslzqtx = false
    
    
    var body: some View {
        VStack {
            CustomNavigationBar(rightImageName: nil, title: "其他设置") {}
            ScrollView {
                VStack(){
                    HStack{
                        Text("显示").font(.system(size: 16)).foregroundStyle(.white)
                        Spacer()
                    }
                    .frame(height: 52)
                    .padding(.horizontal,20)
                    
                    HStack{
                        Text("时间格式").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        ZStack(alignment: .trailing) {
                            Text(selectedTimeType).font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                            Picker("", selection: $selectedTimeType) {
                                ForEach(times, id: \.self) { item in
                                    Text(item)
                                        .foregroundColor(.white)
                                }
                            }
                            .labelsHidden()
                            .pickerStyle(.menu)
                            .frame(height: 50)
                            .environment(\.colorScheme, .dark)
                            .tint(.white)
                            .opacity(0.015)
                        }
                        Image("xiangyou")
                                .resizable()
                                .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    .onChange(of: selectedTimeType) { newValue in
                        if newValue == "12小时制"{
                            viewModel.setTimeDisplay(dateType: 1, timeType: 1)
                        }else {
                            viewModel.setTimeDisplay(dateType: 1, timeType: 2)
                        }
                    }
                    
                    HStack{
                        Text("语言").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        ZStack(alignment: .trailing) {
                            Text(viewModel.currentLanguage).font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                            Picker("", selection: $viewModel.currentLanguage) {
                                ForEach(viewModel.languageList, id: \.self) { item in
                                    Text(item)
                                        .foregroundColor(.white)
                                }
                            }
                            .labelsHidden()
                            .pickerStyle(.menu)
                            .frame(height: 50)
                            .environment(\.colorScheme, .dark)
                            .tint(.white)
                            .opacity(0.015)
                        }
                        
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    .onChange(of: viewModel.currentLanguage) {newValue in
                        if newValue != viewModel.currentLanguageState {
                            if let language = SWUTEDeviceLanguage.fromDescription(newValue),let lan = UTEDeviceLanguage(rawValue:language.rawValue) {
                                viewModel.setLanguageOnly(language: lan)
                            } else {
                                print("未找到匹配的枚举值")
                            }
                        }
                    }
                    
                    HStack{
                        Text("亮屏时间").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        ZStack(alignment: .trailing) {
                            Text(viewModel.lightTime ?? "").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                            Picker("", selection: $viewModel.lightTime) {
                                ForEach(viewModel.lightTimeList, id: \.self) { item in
                                    Text(item).tag(item)
                                        .foregroundColor(.white)
                                }
                            }
                            .labelsHidden()
                            .pickerStyle(.menu)
                            .frame(height: 50)
                            .environment(\.colorScheme, .dark)
                            .tint(.white)
                            .opacity(0.015)
                        }
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    .onChange(of: viewModel.lightTime ?? " ") {newValue in
                        if newValue != viewModel.lightTimeState {
                            viewModel.setLightTime(lightTime: getNumberParam(str: newValue))
                        }
                    }
                    
                    HStack {
                        VStack(alignment: .leading,spacing: 12){
                            Text("每日语录").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                            Text("开启后首页将展示每日语录").font(.system(size: 12)).foregroundStyle(Color(hex: "#8A8A8A"))
                        }
                        Spacer()
                        Toggle("", isOn: $viewModel.dailyQuotesStat)
                            .frame(width: 47,height: 27)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .overlay(Color.clear.contentShape(Rectangle()))
                            .allowsHitTesting(false)
                            
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    HStack {
                        VStack (alignment: .leading,spacing: 12){
                            Text("抬腕亮屏").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                            Text("开启后，将手腕抬起并转向自己时，手表屏幕会自动点亮，且生效时段可自定义")
                                .lineLimit(2)
                                .frame(height:30)
                                .font(.system(size: 12))
                                .foregroundStyle(Color(hex: "#8A8A8A"))
                                
                        }.padding(.trailing,20)
                        Spacer()
                        Toggle("", isOn: $viewModel.switchState)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                            
                    }
                    .frame(height: 91)
                    .padding(.horizontal,20)
                    .onChange(of: viewModel.switchState) {newValue in
                        if newValue != viewModel.watchStatus {
                            viewModel.setScreenAutoLight(enable: newValue)
                        }
                    }
                    HStack {
                        Text("生效时段").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("07:00 上午-10:00 下午").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 45)
                    .padding(.horizontal,20)

                    Spacer()
                }
//                .frame(height: 430)
                .background(Color(hex: "#1E1E1E"))
                .cornerRadius(16)
                .padding(.horizontal,12)
                .padding(.top,12)
                
                VStack {
                    HStack{
                        Text("控制").font(.system(size: 16)).foregroundStyle(.white)
                        Spacer()
                    }
                    .frame(height: 52)
                    .padding(.horizontal,20)
                    
                    HStack {
                        VStack(alignment: .leading,spacing: 12){
                            Text("手表控制手机音乐").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                            Text("开启权限以将手机音乐信息同步显示在手表上").font(.system(size: 12)).foregroundStyle(Color(hex: "#8A8A8A"))
                        }
                        Spacer()
                        Toggle("", isOn: $iskzyy)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    HStack {
                        Text("来电拒接短信设置").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 45)
                    .padding(.horizontal,20)
                    Spacer()
                }
//                .frame(height: 176)
                .background(Color(hex: "#1E1E1E"))
                .cornerRadius(16)
                .padding(.horizontal,12)
                .padding(.top,12)
                
                VStack {
                    HStack{
                        Text("场景模式").font(.system(size: 16)).foregroundStyle(.white)
                        Spacer()
                    }
                    .frame(height: 52)
                    .padding(.horizontal,20)
                    
                    HStack {
                        Text("省电模式").font(.system(size: 16)).foregroundStyle(.white)
                        Spacer()
                        Toggle("", isOn: $issdms)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                        
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    .onChange(of:issdms) {newValue in
                        if newValue {
                            
                            
                            
                        }else {
                            
                            
                            
                        }
                    }
                    HStack {
                        Text("勿扰模式时段").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("关闭").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    Spacer()
                }
//                .frame(height: 162)
                .background(Color(hex: "#1E1E1E"))
                .cornerRadius(16)
                .padding(.horizontal,12)
                .padding(.top,12)
                
                
                VStack {
                    HStack{
                        Text("天气信息").font(.system(size: 16)).foregroundStyle(.white)
                        Spacer()
                    }
                    .frame(height: 52)
                    .padding(.horizontal,20)
                    
                    HStack {
                        VStack(alignment: .leading,spacing: 12){
                            Text("天气信息").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                            Text("每小时自动更新设备天气信息").font(.system(size: 12)).foregroundStyle(Color(hex: "#8A8A8A"))
                        }
                        Spacer()
                        Toggle("", isOn: $istqxx)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    HStack {
                        Text("所在地").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("东莞").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    Spacer()
                }
//                .frame(height: 186)
                .background(Color(hex: "#1E1E1E"))
                .cornerRadius(16)
                .padding(.horizontal,12)
                .padding(.top,12)
                
                VStack {
                    HStack{
                        Text("健康").font(.system(size: 16)).foregroundStyle(.white)
                        Spacer()
                    }
                    .frame(height: 52)
                    .padding(.horizontal,20)
                    
                    HStack {
                        Text("SOS紧急求助").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Toggle("", isOn: $issos)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                        
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    HStack {
                        Text("全天压力").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Toggle("", isOn: $viewModel.autoStressState)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                        
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    .onChange(of: viewModel.autoStressState) {newValue in
                        if newValue != viewModel.autoStress{
                            viewModel.setAutoStress(enable: newValue)
                        }
                    }
                    
                    HStack {
                        VStack(alignment: .leading,spacing: 12) {
                            Text("久坐提醒").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                            Text("有效时段为10:00-22:00").font(.system(size: 12)).foregroundStyle(Color(hex: "#8A8A8A"))
                        }
                        Spacer()
                        Toggle("", isOn: $viewModel.activityState)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                        
                    }
                    .frame(height: 52)
                    .padding(.horizontal,20)
                    .onChange(of: viewModel.activityState) {newValue in
                        if newValue != viewModel.activity {
                            viewModel.setActivityRemind(enable: newValue, interval: 0, startTime: 0, endTime: 0, cycle: 0)
                        }
                    }
                    HStack {
                        VStack(alignment: .leading,spacing: 12) {
                            Text("心率预警").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                            Text("您的心率上线默认值为195BPM").font(.system(size: 12)).foregroundStyle(Color(hex: "#8A8A8A"))
                        }
                        Spacer()
                        Toggle("", isOn: $viewModel.heartrateState)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                    }
                    .frame(height: 52)
                    .padding(.horizontal,20)
                    .onChange(of: viewModel.heartrateState) {newValue in
                        if newValue != viewModel.heartrate{
                            viewModel.setHeartrateRaiseRemindSwitch(enable: newValue)
                        }
                    }
                    HStack {
                        VStack(alignment: .leading,spacing: 12) {
                            Text("生理周期提醒").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                            Text("有经期管理、备孕调理、两性健康三种提醒模式").font(.system(size: 12)).foregroundStyle(Color(hex: "#8A8A8A"))
                        }
                        Spacer()
                        Toggle("", isOn: $isslzqtx)
                            .toggleStyle(SwitchToggleStyle(tint: Color(hex: "#00E9DA")))
                            .frame(width: 47,height: 27)
                    }
                    .frame(height: 52)
                    .padding(.horizontal,20)
                    .onChange(of: isslzqtx) {newValue in
                        if newValue {
                            viewModel.setMenstrualNotifyAbility(status: 31)
                        }else {
                            viewModel.setMenstrualNotifyAbility(status: 0)
                        }
                    }
                    HStack {
                        Text("生理周期天数").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("28").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    HStack {
                        Text("生理期天数").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("5").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    
                    HStack {
                        Text("上次生理期开始日期").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("2025/2/22").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    HStack {
                        Text("提醒模式").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    HStack {
                        Text("提醒时间").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("10:00 上午").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    HStack {
                        Text("生理周期日历").font(.system(size: 16)).foregroundStyle(Color(hex: "#CACACA"))
                        Spacer()
                        Text("").font(.system(size: 16)).foregroundStyle(Color(hex: "#FFFFFF"))
                        Image("xiangyou")
                            .resizable()
                            .frame(width: 16,height: 16)
                            .onTapGesture {
                                appState.navigateTo(.cycleCalender)
                            }
                    }
                    .frame(height: 48)
                    .padding(.horizontal,20)
                    
                    Spacer()
                    
                }
                .background(Color(hex: "#1E1E1E"))
                .cornerRadius(16)
                .padding(.horizontal,12)
                .padding(.top,12)
                
              
            }
            HStack {
                Button {
                    viewModel.resetFactory(type: 0)
                } label: {
                    Text("恢复出厂设置")
                        .font(.system(size: 16))
                        .foregroundStyle(Color(hex: "#242424"))
                }
            }
            .frame(maxWidth: .infinity, alignment: .center)
            .frame(height: 44)
            .gradientBackground(colors: [Color(hex: "#B2B2B2"),Color(hex: "#B2B2B2")], type: .linear(start: .leading, end: .trailing), cornerRadius: 22.0)
            .padding(.horizontal,12)
            .padding(.bottom,20)
            .toast(isPresented: $showToast) {
                ShowMessageView(message: viewModel.errorStr)
            }
            .onChange(of: viewModel.errorStr) {
                newValue in
                if let errorMessage = newValue, !errorMessage.isEmpty {
                    showToast = true
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                        showToast = false
                        viewModel.errorStr = nil
                    }
                }
            }
        }
        .navigationBarHidden(true)
        .background(Color(hex: "#0A0A0A"))
        .edgesIgnoringSafeArea(.all)
        .onAppear {
            viewModel.getScreenAutoLightState()
            viewModel.getHeartrateRaiseRemindSwitch()
            viewModel.getAutoStress()
            viewModel.readLightTime()
            viewModel.readLightSetTimeList()
            viewModel.getActivityRemind()
            viewModel.readSupportLanguage()
            viewModel.readCurrentLanguage()
        }
    }
    
    func getNumberParam(str:String)->Int {
        let number = str.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()
        print(number)
        return Int(number) ?? 0
    }
 
}

//#Preview {
//    OtherSettingView()
//}
